package com.example.take_out_dish.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.take_out_dish.common.Result;
import com.example.take_out_dish.common.lang.dto.DishDto;
import com.example.take_out_dish.common.lang.vo.DishVo;
import com.example.take_out_dish.entity.Dish;
import com.example.take_out_dish.entity.DishFlavor;
import com.example.take_out_dish.entity.SetmealDish;
import com.example.take_out_dish.exceptions.BusinessException;
import com.example.take_out_dish.exceptions.enumetation.ResponseEnum;
import com.example.take_out_dish.mapper.DishFlavorMapper;
import com.example.take_out_dish.mapper.DishMapper;
import com.example.take_out_dish.mapper.SetmealDishMapper;
import com.example.take_out_dish.mapper.SetmealMapper;
import com.example.take_out_dish.service.DishFlavorService;
import com.example.take_out_dish.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    @Resource
    private DishFlavorMapper dishFlavorMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private DishFlavorService dishFlavorService;

    @Override
    public void saveWithFlavor(DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtil.copyProperties(dishDto, dish, "createTime", "updateTime");
        this.save(dish);
        String dishId = dish.getId();
        // 向口味表插入n条数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        if(CollUtil.isNotEmpty(flavors)) {
            flavors.forEach(s->s.setDishId(dishId));
            dishFlavorMapper.insert(flavors);
        }
    }

    @Override
    public void delete(List<String> ids) {
        // 启售的商品数量
        long count = this.listByIds(ids).stream().filter(s -> s.getStatus() > 0).count();
        if (count > 0) {
            throw new BusinessException(ResponseEnum.DISH_ERROR);
        }

        count = setmealDishMapper.selectCount(new LambdaQueryWrapper<SetmealDish>()
                .in(SetmealDish::getDishId, ids));
        if (count > 0) {
            throw new BusinessException(ResponseEnum.DISH_ERROR);
        }
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.delete(new LambdaQueryWrapper<DishFlavor>()
                .in(DishFlavor::getDishId, ids));
    }

    @Override
    public DishVo getByIdWithFlavor(String id) {
        Dish dish = dishMapper.selectById(id);
        if(ObjectUtil.isNull(dish)) {
            throw new BusinessException(ResponseEnum.DISH_ERROR);
        }
        List<DishFlavor> byDishId = dishFlavorMapper.selectList(new LambdaQueryWrapper<DishFlavor>()
                .eq(DishFlavor::getDishId, id));
        DishVo dishVo = new DishVo();
        BeanUtil.copyProperties(dish, dishVo, "createTime", "updateTime");

        return dishVo;
    }

    @Override
    public void update(DishDto dishDto) {
        Dish dish = new Dish();
        BeanUtil.copyProperties(dishDto, dish);
        dishMapper.updateById(dish);
        // 删除原有口味数据
        dishFlavorMapper.delete(new LambdaQueryWrapper<DishFlavor>().in(DishFlavor::getDishId,
                Collections.singletonList(dishDto.getId())));// 创建单元素的不变列表
        // 重新插入口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        if(CollUtil.isNotEmpty(flavors)) {
            flavors.forEach(s->s.setDishId(dishDto.getId()));
            dishFlavorMapper.insert(flavors);
        }

    }

    @Override
    public List<DishVo> listWithFlavor(String categoryId) {
        //如果不存在,查询数据库返回,将查询到的数据放入Redis
        List<DishVo> dishVOList = dishMapper.getDishByCategoryId(categoryId);
        return dishVOList;
    }

    @Override
    public List<Dish> selectByCategoryId(String categoryId) {
        return dishMapper.selectList(new LambdaQueryWrapper<Dish>()
                .eq(Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, "1"));
    }

    @Override
    public void updateStatus(Integer status, String id) {
        Dish dish = dishMapper.selectById(id);
        if(dish == null) {
            throw new BusinessException(ResponseEnum.NOT_EXIST);
        }
        dish.setStatus(status);
        dishMapper.updateById(dish);
    }


    /**
     * 根据套餐id联立套餐菜品表查询菜品数据
     * @param setmealId
     * @return
     */
    @Override
    public List<Dish> selectBySetmealId(String setmealId) {
        return dishMapper.selectBySetmealId(setmealId);
    }
}
